package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-02-27
 * Time: 16:21
 */
//写一个泛型类，这个类里面，可以求数组的最大值，数组元素并不一定是整数
class Alg<E extends Comparable<E>>{
    public E FindMax(E[] array) {
        E max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0) {
                max = array[i];
            }
        }
        return max;
    }
}

//泛型方法：
class Alg2{
    public<E extends Comparable<E>> E FindMax(E[] array) {
        E max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0) {
                max = array[i];
            }
        }
        return max;
    }
}
class Alg3{
    public static <E extends Comparable<E>> E FindMax(E[] array) {
        E max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0) {
                max = array[i];
            }
        }
        return max;
    }
}
public class demo1 {
    public static void main(String[] args) {
        Integer[] integers = new Integer[]{1,2,3,4,5};
        int ret = Alg3.<Integer>FindMax(integers);
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        Alg2 alg2 = new Alg2();
        Integer[] integers = new Integer[]{1,2,3,4,5};
        int ret = alg2.<Integer>FindMax(integers);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        Alg<Integer> alg = new Alg<Integer>();
        Integer[] integers = new Integer[]{1,2,3,4,5};
        System.out.println(alg.FindMax(integers));

        Alg<String> alg1 = new Alg<String>();
        String[] strings = new String[]{"abc","cba","zoo"};
        System.out.println(alg1.FindMax(strings));
    }
}
